package entity

import (
	"time"
	"xr-game-server/core/constants/db"
	"xr-game-server/core/migrate"
	"xr-game-server/core/snowflake"
	"xr-game-server/core/syndb"
)

const (
	TbPlayerTask = "player_tasks"
)

const (
	PlayerTaskCfgId  db.TbCol = "task_cfg_id"
	PlayerTaskStatus db.TbCol = "status"
	PlayerTaskRoleId db.TbCol = "role_id"
)

type PlayerTask struct {
	migrate.OneModel
	TaskCfgId uint64 `gorm:"comment:任务配置表id"`
	Status    uint8  `gorm:"comment:状态 0 未完成 1 已完成 2已领取"`
	RoleId    uint64 `gorm:"comment:角色id"`
}

func initTask() {
	syndb.RegLazyWithMiddle(TbPlayerTask, db.CreatedAtName)
	syndb.RegLazyWithMiddle(TbPlayerTask, db.UpdatedAtName)

	syndb.RegLazyWithMiddle(TbPlayerTask, PlayerTaskCfgId)
	syndb.RegLazyWithMiddle(TbPlayerTask, PlayerTaskStatus)
	syndb.RegLazyWithMiddle(TbPlayerTask, PlayerTaskRoleId)

	migrate.AutoMigrate(&PlayerTask{})
}

func NewPlayerTask(roleId uint64, taskCfgId uint64) *PlayerTask {
	ret := &PlayerTask{}
	ret.ID = snowflake.GetId()
	ret.SetRoleId(roleId)
	ret.SetTaskCfgId(taskCfgId)
	ret.SetCreatedAt(time.Now())
	ret.SetCreatedAt(time.Now())
	return ret
}

func (receiver *PlayerTask) SetRoleId(roleId uint64) {
	receiver.RoleId = roleId
	syndb.AddDataToLazyChan(TbPlayerTask, PlayerTaskRoleId, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: roleId,
	})
}
func (receiver *PlayerTask) SetTaskCfgId(val uint64) {
	receiver.TaskCfgId = val
	syndb.AddDataToLazyChan(TbPlayerTask, PlayerTaskCfgId, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}
func (receiver *PlayerTask) SetStatus(stats uint8) {
	receiver.Status = stats
	receiver.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbPlayerTask, PlayerTaskStatus, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: stats,
	})
}
func (receiver *PlayerTask) SetCreatedAt(val time.Time) {
	receiver.CreatedAt = val
	syndb.AddDataToLazyChan(TbPlayerTask, db.CreatedAtName, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}

func (receiver *PlayerTask) SetUpdatedAt(val time.Time) {
	receiver.UpdatedAt = val
	syndb.AddDataToLazyChan(TbPlayerTask, db.UpdatedAtName, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}
